﻿//-----------------------------------------------------------------------
// <copyright file="DataImporter.cs" company="FER">
//     Copyright (c) All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace TestDataImporter
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Reflection;
    using System.Windows.Media.Imaging;

    using NMBP.DAL;
    using NMBP.Models;
    using NMBP.Models.Converters;

    /// <summary>
    /// Class used for importing test data to database
    /// </summary>
    public class DataImporter
    {
        #region Departments
        /// <summary>
        /// Import fake departments to database
        /// </summary>
        /// <param name="databaseFacade">Instance of database facade</param>
        public void ImportDepartments(DbFacade databaseFacade)
        {
            Console.WriteLine("Importing Departments.");
            var departments = new List<Department>
                {
                    new Department
                        {
                            Name = "Prodaja",
                            ManagerUsername = string.Empty,
                            Employees = null,
                        },
                    new Department
                        {
                            Name = "Financije",
                            ManagerUsername = null,
                            Employees = null,
                        }
                };

            foreach (var department in departments)
            {
                databaseFacade.SaveDepartment(department);
            }

            Console.WriteLine("Departments imported.");
        }
        #endregion

        #region Persons
        /// <summary>
        /// Import fake persons to database
        /// </summary>
        /// <param name="databaseFacade">Instance of database facade</param>
        public void ImportPersons(DbFacade databaseFacade)
        {
            Console.WriteLine("Importing persons");

            var departments = databaseFacade.GetDepartments();

            var persons = new List<Person>()
                {
                    new Worker
                        {
                            Contact = "domagoj.rukavina@gmail.com",
                            Department = departments[0],
                            IsManager = false,
                            Name = "Domagoj",
                            Sallary = 10000,
                            Surname = "Rukavina",
                            Username = "drukavina",
                            Password = "0000",
                            VpnNumber = "100",
                            IsAdmin = true,
                        },
                    new Worker
                        {
                            Contact = "filip.rodik@gmail.com",
                            Department = departments[1],
                            IsManager = false,
                            Name = "Filip",
                            Sallary = 10000,
                            Surname = "Rodik",
                            Username = "frodik",
                            Password = "1111",
                            VpnNumber = "100",
                            IsAdmin = false
                        },
                    new Customer
                        {
                            Contact = "party.beast@gmail.com",
                            Name = "Cosmo",
                            Surname = "Cramer",
                            Username = "ccramer",
                            Password = "1111",
                            BankCardNumber = "0000-1111",
                            Cart = null,
                            PrefferedPayment = NMBP.Models.Enums.PaymentMethod.Cash
                        }
                };

            foreach (var person in persons)
            {
                databaseFacade.SavePerson(person);
            }

            Console.WriteLine("Persons imported");
        }

        /// <summary>
        /// Tests whether there are some persons in db
        /// </summary>
        /// <param name="databaseFacade">instance of database facade</param>
        public void TestWriteToDb_Persons(DbFacade databaseFacade)
        {
            var itemsInDb = databaseFacade.GetPersons();
            foreach (var item in itemsInDb)
            {
                Console.WriteLine("{0}, {1}, {2}, {3}, {4}", item.Name, item.Surname, item.Username, item.Password, item.Contact);
            }

            Console.ReadLine();
        }
        #endregion

        #region Products
        /// <summary>
        /// Imports products to database
        /// </summary>
        /// <param name="databaseFacade">database facade</param>
        public void ImportProducts(DbFacade databaseFacade)
        {
            Console.WriteLine("Importing products");
            var imagesFolder = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory + "\\Images";

            var listOfProducts = new List<Product>
                {
                    new Product
                        {
                            Availability = NMBP.Models.Enums.ProductAvailability.InStock,
                            Description = "Proizvod1",
                            Image = MyImageConverter.ImageToByteArray(new Bitmap(imagesFolder + "\\product1.jpg")),
                            InStock = 10,
                            Price = 100.0f,
                        },
                    new Product
                        {
                            Availability = NMBP.Models.Enums.ProductAvailability.AvailableIn24H,
                            Description = "Proizvod2",
                            Image = MyImageConverter.ImageToByteArray(new Bitmap(imagesFolder + "\\product2.jpg")),
                            InStock = 20,
                            Price = 200.0f,
                        },
                    new Product
                        {
                            Availability = NMBP.Models.Enums.ProductAvailability.NotAvailable,
                            Description = "Proizvod3",
                            Image = MyImageConverter.ImageToByteArray(new Bitmap(imagesFolder + "\\product3.jpg")),
                            InStock = 30,
                            Price = 300.0f,
                        }
                };

            foreach (var product in listOfProducts)
            {
                databaseFacade.SaveProduct(product);
            }

            Console.WriteLine("Products imported");
        }

        #endregion

        #region Suppliers
        /// <summary>
        /// Imports suppliers to database
        /// </summary>
        /// <param name="databaseFacade">instance of database facade</param>
        public void ImportSuppliers(DbFacade databaseFacade)
        {
            Console.WriteLine("Importing suppliers");

            var suppliers = new List<Supplier>
                {
                    new Supplier { AccountNumber = "0123456789", Contact = "someone@supplier1.com", Name = "Supplier1", IsActive = true },
                    new Supplier { AccountNumber = "1111111111", Contact = "someone@supplier2.com", Name = "Supplier2", IsActive = true },
                    new Supplier { AccountNumber = "2222222222", Contact = "someone@supplier3.com", Name = "Supplier3", IsActive = true }
                };

            foreach (var supplier in suppliers)
            {
                databaseFacade.SaveSupplier(supplier);
            }

            Console.WriteLine("Suppliers imported");
        }
        #endregion
    }
}
